package com.tx.review.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * 反射
 *
 * @author ：tx
 * @version:
 */
public class ReflectDemo {
    public static void main(String[] args) throws Exception {
        //获取Class对象
//        testClass();

        //通过反射创建对象
//        testCreate();

        //测试部分反射api
//        testAPI();

        //测试利用反射调用私有方法
        testCallMethod();
    }

    private static void testCallMethod() throws Exception {
        Class pClas = Person.class;
//        Method method = pClas.getMethod("testPrivate"); //找不到私有方法
        Method method = pClas.getDeclaredMethod("testPrivate");
        //true:在使用的时候取消检查
        method.setAccessible(true);
        Person person = (Person) pClas.newInstance();
        for (int i = 1; i <=16 ; i++) {
            method.invoke(person);
        }

    }

    private static void testAPI() {
        Class pClas = Person.class;
        //包括父类的方法
        Method[] methods = pClas.getMethods();
        System.out.println(">>>>>>>public方法如下");
        for (Method method : methods) {
            System.out.println(method);
        }
        System.out.println("===========================");
        System.out.println(">>>>>>>private方法：");
        //返回本对象的方法，包括私有
        Method[] declaredMethods = pClas.getDeclaredMethods();
        for (Method method : declaredMethods) {
            System.out.println(method);
        }

    }

    private static void testCreate() throws Exception {
        //方式1
       /* Class pClas = Person.class;
        Person person = (Person) pClas.newInstance();
        System.out.println("============================");
        person.hello();*/

        //方式2：
        //无参
       /* Class pClas = Person.class;
        Constructor constructor = pClas.getConstructor();
        Person person = (Person) constructor.newInstance();
        System.out.println("============================");
        person.hello();*/

        //有参
        Class pClas = Person.class;
        Constructor constructor = pClas.getConstructor(String.class,Integer.class);
        Person person = (Person) constructor.newInstance("mercury",18);
        System.out.println("============================");
        person.printName();
    }

    private static void testClass() throws ClassNotFoundException {

//        Class<Person> p2 = Person.class; //什么也不打印

//        Class p1 = Class.forName("com.tx.review.reflect.Person"); //打印static

        //都打印了
        Person person = new Person();
        Class p3 = person.getClass();
    }
}

class Person{


    private String name;
    private Integer age;


    static{
        System.out.println("static");
    }


    {
        System.out.println("dynamic");
    }

    public Person() {
        System.out.println("Construction");
    }

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }


    public void hello(){
        System.out.println("hi~");
    }

    public void printName(){
        System.out.println("name:" + this.name);
    }

    private void testPrivate(){
        System.out.println("我是private方法");
    }
}
